<?php
/**
 * 
 * @author Mazvv
 * @package Extenders
 */

class Menucontainer {
	private $ciObject;
	private $a_positionRid;
	private $a_menuItems = array();
	private $a_objectsList = array();
	private $a_currentItemRid = null;
	
	public function __construct($p_positionRid) {
		$this->ciObject = &get_instance();	
		$this->ciObject->load->model('menucontainer_model');
		$this->a_positionRid = $p_positionRid;
		$this->GetContainerItems();
		$this->ciObject->lang->load('menucontainer');
	}
	
	public function GetContainerItems(){
		$result = $this->ciObject->menucontainer_model->GetMenusList($this->a_positionRid);
		if(!$result) return;
		foreach($result as $row){
			$this->a_menuItems[] = $row;
			# определяем RID пункта меню
			if($this->ciObject->uri->uri_string() && strpos($this->ciObject->uri->uri_string(), '/'.$row->item_controller)===0) $this->a_currentItemRid = $row->rid; 
		}
		//echo $this->a_currentItemRid;
		return;
	}
	
	public function UpdateContainerItems(){
		$this->GetContainerItems();
	}
	
	public function RenderVerticalDropDownMenu(){
		return $this->ciObject->load->view('menucontainer/vertical_dropdown_menu', $this->a_objectsList, True);
	}
	
	public function RenderVerticalTreeMenu(){
		$this->a_objectsList['M2_MENU_ITEMS'] = $this->a_menuItems;
		$this->a_objectsList['M2_OPEN_ALL_TITLE'] = $this->ciObject->lang->line('M2_OPEN_ALL_TITLE');
		$this->a_objectsList['M2_CLOSE_ALL_TITLE'] = $this->ciObject->lang->line('M2_CLOSE_ALL_TITLE');
		return $this->ciObject->load->view('menucontainer/vertical_tree_menu', $this->a_objectsList, True);
	}
	
	public function RenderHorizontalDropDownMenu(){
		
	}
	
	public function GetItemRights(){
		if($this->a_currentItemRid) foreach($this->a_menuItems as $item){ if($item->rid == $this->a_currentItemRid) return $item->item_rights;}
		return false;
	}
	
	public function GetItemArea(){
		if($this->a_currentItemRid) foreach($this->a_menuItems as $item){ if($item->rid == $this->a_currentItemRid) return $item->item_area;}
		return false;
	}
	
	public function GetItemConstantSearchRule(){
		if($this->a_currentItemRid) foreach($this->a_menuItems as $item){ if($item->rid == $this->a_currentItemRid) return $item->constant_search_rule;}
		return false;
	}

	public function GetItemDefaultSortRule(){
		if($this->a_currentItemRid) foreach($this->a_menuItems as $item){ if($item->rid == $this->a_currentItemRid) return $item->default_sort_rule;}
		return false;
	}
}
?>